package design.pattern.application.CommandAndChain;

/**
 * @program JavaBase
 * @description:
 * @author: zhanglu
 * @create: 2020-05-22 14:36:00
 */
public class Invoker {

    //执行命令
    public String exec(String commandStr){
        String result = "";
        CommandVo commandVo = new CommandVo(commandStr);
        if(CommandEnum.getNames().contains(commandVo.getCommandName())){
            String className = CommandEnum.valueOf(commandVo.getCommandName()).getValue();
            try {
                Command command = (Command) Class.forName(className).newInstance();
                result = command.execute(commandVo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            result = "无法执行命令，请检查命令格式";
        }
        return result;
    }

}
